﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AddService : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    private string getFileFromFileUpload()
    {
        string fileUpload = "";
        string str = "";
        try
        {
            FileUpload upload = ImageServiceUpload;

            if (upload.HasFile && upload.PostedFile.ContentLength < 2097152)
            {
                str += "Uploading file: " + upload.FileName;

                upload.SaveAs(MapPath("~/Images/Service/" + upload.FileName));
                str += "<br/>Saved As: " + upload.PostedFile.FileName;
                str += "<br/>File Type: " + upload.PostedFile.ContentType;
                str += "<br/>File Length (bytes): " + upload.PostedFile.ContentLength;
                str += "<br/>PostedFile File Name: " + upload.PostedFile.FileName;
                fileUpload = upload.FileName;
            }
            else if (upload.HasFile && upload.PostedFile.ContentLength > 2097152)
            {
                str = "File upload must be smaller than 2 mb";
            }
            else
            {
                str = "No file uploaded.";
            }


        }
        catch (Exception ex)
        {

            str += "<br/><b>Error</b><br/>Unable to Insert " + ex.Message;
        }
        lblMessage.Text = str;
        return fileUpload;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
	    {
            try
            {
                string imageFile = getFileFromFileUpload();
                string serviceName = txtServiceName.Text;
                string serviceDescription = txtDescription.Text;
                if (DALService.AddService(serviceName, imageFile, serviceDescription))
                {
                    lblMessage.Text = "Insert Successful";
                }
                else
                {
                    lblMessage.Text = "Insert Fail";
                }
                lblImageFile.Text = imageFile;
            }
            catch
            {
                lblMessage.Text = "Insert Fail";
            }
            
	    }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtServiceName.Text = "";
        txtDescription.Text = "";
        lblImageFile.Text = "";
    }
}
